//
//  HNSH_LoginInputView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_LoginInputView.h"

@interface HNSH_LoginInputView ()

/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* lineView */
@property (nonatomic, strong) UIView *lineView;
/* textField */
@property (nonatomic, strong) UITextField *textField;
/* eyeButton */
@property (nonatomic, strong) UIButton *eyeButton;

@end

@implementation HNSH_LoginInputView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type {
    self = [super initWithFrame:frame];
    if (self) {
        _type = type;
        [self configUI];
    }
    return self;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.textField.placeholder = placeholder;
    NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textField.placeholder];
    [placeholderString addAttribute:NSForegroundColorAttributeName
                            value:[UIColor colorWithHexString:@"#8E8E93"]
                            range:NSMakeRange(0, self.textField.placeholder.length)];
    [placeholderString addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:14]
                            range:NSMakeRange(0, self.textField.placeholder.length)];
    self.textField.attributedPlaceholder = placeholderString;
}

- (void)setHiddenText:(BOOL)hiddenText {
    _hiddenText = hiddenText;
    if (hiddenText) {
        self.textField.secureTextEntry = YES;
        self.eyeButton.hidden = NO;
    }
}

- (void)configUI {
    self.backgroundColor = [UIColor colorWithHexString:@"#F2F3F6"];
    self.layer.cornerRadius = self.height/2.0;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.textField];
    [self addSubview:self.sendButton];
    [self addSubview:self.eyeButton];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(65);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(16);
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(98);
        make.height.mas_equalTo(28);
    }];
    
    if (self.type == 1) {
        self.titleLabel.hidden = self.lineView.hidden = NO;
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(79);
            make.top.bottom.mas_equalTo(0);
            make.right.mas_equalTo(-28);
        }];
    } else if (self.type == 2) {
        self.sendButton.hidden = NO;
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(28);
            make.top.bottom.mas_equalTo(0);
            make.right.mas_equalTo(-118);
        }];
    } else {
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(28);
            make.top.bottom.mas_equalTo(0);
            make.right.mas_equalTo(-28);
        }];
        
        [self.eyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(24);
            make.height.mas_equalTo(24);
        }];
    }
}

#pragma mark lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"+86";
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.hidden = YES;
    }
    return _titleLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#DADADA"];
        _lineView.hidden = YES;
    }
    return _lineView;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont systemFontOfSize:14];
        WeakSelf(weakSelf);
        [[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UITextField *field = x;
            if (weakSelf.fieldBlock) {
                weakSelf.fieldBlock(field.text);
            }
        }];
    }
    return _textField;
}

- (UIButton *)sendButton {
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_sendButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _sendButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sendButton.hidden = YES;
        WeakSelf(weakSelf);
        [[_sendButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *button = x;
            if (weakSelf.buttonBlock) {
                weakSelf.buttonBlock(button);
            }
        }];
    }
    return _sendButton;
}

- (UIButton *)eyeButton {
    if (!_eyeButton) {
        _eyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_eyeButton setImage:[UIImage imageNamed:@"base_text_field_eye_off"] forState:UIControlStateNormal];
        [_eyeButton setImage:[UIImage imageNamed:@"base_text_field_eye_on"] forState:UIControlStateSelected];
        _eyeButton.hidden = YES;
        WeakSelf(weakSelf);
        [[_eyeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *button = x;
            button.selected = !button.selected;
            weakSelf.textField.secureTextEntry = !button.isSelected;
        }];
    }
    return _eyeButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
